package com.uchengwang.ui.cm_ui.uu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.uu.bean.SystemNotifyBean;
import com.uchengwang.ui.cm_ui.uu.ui.ClubInviteActivity;
import com.uchengwang.ui.cm_ui.uu.ui.CmtMsgAndFeedbackActivity;
import com.uchengwang.ui.cm_ui.uu.ui.GroupNotifyActivity;
import com.uchengwang.ui.utils.DateUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/10.
 */

public class SystemNotifyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context context;
    private List<SystemNotifyBean> list = new ArrayList<>();
    public SystemNotifyAdapter(Context context){
        this.context = context;
    }

    public void setData(List<SystemNotifyBean> slist){
        this.list = slist;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_system_notify, parent, false);
        return new NotifyVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        NotifyVH holder = (NotifyVH) h;
        final SystemNotifyBean bean = list.get(position);

        ImageManager.getInstance().loadImage(context, holder.msg_head, bean.getLogo(), ImageManager.ImageType.CIRCLE);
        holder.msg_name.setText(bean.getTitle());
        holder.msg_content.setText(bean.getContent());
        if (bean.getTime() >0){
            holder.msg_time.setText(DateUtil.getDateUtil().getTimeStringDate(bean.getTime()));
        }

        if (bean.isNoRead()){
            holder.msg_noread.setVisibility(View.VISIBLE);
        }else {
            holder.msg_noread.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = null;
                switch (bean.getTitle()){
                    case "Uni团队"://uni通知
                        intent = new Intent(context, CmtMsgAndFeedbackActivity.class);
                        if (UChengApplication.userRole == BaseConfig.UNI_ROLE || UChengApplication.userRole == BaseConfig.EDITOR_ROLE ||
                                UChengApplication.userRole == BaseConfig.ZONG_ROLE){
                            intent.putExtra(BaseConfig.TYPE, 3);
                        }else {
                            intent.putExtra(BaseConfig.TYPE, 2);
                        }
                        break;
                    case "参与号"://参与号
                        intent = new Intent(context, CmtMsgAndFeedbackActivity.class);
                        intent.putExtra(BaseConfig.TYPE, 1);
                        break;
                    case "群助手"://群助手
                        intent = new Intent(context, GroupNotifyActivity.class);
                        break;
                    case "社团号"://社团号
                        intent = new Intent(context, ClubInviteActivity.class);
                        break;
                }

                if (intent != null){
                    context.startActivity(intent);
                    bean.setNoRead(false);
                    notifyDataSetChanged();
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class NotifyVH extends RecyclerView.ViewHolder{
        private View itemView;
        private ImageView msg_head;
        private TextView msg_noread, msg_name, msg_time, msg_content;

        public NotifyVH(View view) {
            super(view);

            AutoUtils.autoSize(view);

            itemView = view;

            msg_head = (ImageView) view.findViewById(R.id.system_head);
            msg_noread = (TextView) view.findViewById(R.id.system_noread);
            msg_name = (TextView) view.findViewById(R.id.system_name);
            msg_time = (TextView) view.findViewById(R.id.system_time);
            msg_content = (TextView) view.findViewById(R.id.system_content);

        }
    }
}
